package com.ycdr.leetcode.stack.example;

public class StackX {
    private int top;
    private int maxSize;
    private Object[] stackArray;

    public StackX(int i){
        maxSize=i;
        stackArray=new Object[i];
        top=-1;
    }

    public void push(Object o){
        if (isFull()){
            throw new RuntimeException("StackX is Overflow");
        }
        stackArray[++top]=o;
    }

    public Object pop(){
        if (isEmpty()){
            throw new RuntimeException("StackX is Empty");
        }
        return stackArray[top--];
    }

    public Object peek(){
        if (isEmpty()){
            throw new RuntimeException("StackX peek is Empty");
        }
        return stackArray[top];
    }

    public boolean isEmpty(){
        return top==-1;
    }

    public boolean isFull(){
        return top==maxSize-1;
    }

    public static void main(String[] args) {
        StackX x=new StackX(5);
        System.out.println(x.isEmpty());
        System.out.println(x.isFull());
        x.push(1);
        x.push(2);
        x.push(3);
        x.push(4);
        x.push(5);
        System.out.println(x.isEmpty());
        System.out.println(x.isFull());
        x.pop();
        x.pop();
        x.pop();
        x.pop();
        x.pop();
        System.out.println(x.isEmpty());
        System.out.println(x.isFull());
        x.push(10);
        System.out.println(x.pop());
        System.out.println(x.peek());
    }
}
